package pure.ui.core 
{
	import pure.ui.groups.GroupBase;
	import pure.ui.groups.IGroupItem;
	import pure.ui.skins.ISkin;
	import pure.ui.skins.RetractileSkin;
	import pure.ui.skins.Skin;
	import flash.display.Bitmap;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * 文本标签按钮基类
	 * @author Des
	 * 
	 * <TODO>坐标为组件中心
	 */
	
public class LabelComponent extends Component implements IGroupItem
{
	public function LabelComponent(x:Number, y:Number,size:Point, skin:ISkin,hasLabel:Boolean) 
	{
		super(x, y, skin);
		if (size)
		{
			setSize(size.x, size.y);
		}
		else 
		{
			setSize(WIDTH, HEIGHT);
		}
		_hasLabel = hasLabel;
	}
	
	
	
	/*default*/
	
	protected static const WIDTH:Number = 24;	// 默认宽
	
	protected static const HEIGHT:Number = 24;	// 默认高
	
	/*model*/	
	
	protected var _id:String;
	
	protected var _label:String;
	
	protected var _hasLabel:Boolean;
	
	protected var _group:GroupBase;
	
	/*cache*/
	
	protected var _bitmap:Bitmap;	// 位图
	
	protected var _textField:TextField;	// 按钮文本

	protected var _textFormat:TextFormat;	// 文本样式
	
	protected var _padding:Number;


	
	/**
	 * 设置标签样式
	 **/
	public function setLabelFormat(font:String = null, color:uint = 0, size:int = 12, bold:Boolean = false):void
	{
		if (!_textFormat)
		{
			_textFormat = new TextFormat(font, size, color, bold);
		}
		if (_textField)
		{
			_textField.setTextFormat(_textFormat);
			_textField.defaultTextFormat = _textFormat;
		}
	}
	
	/**
	 * 毁灭
	 */
	override public function destroy():void
	{
		super.destroy();
		if (_skin is RetractileSkin)
		{
			this._bitmap.bitmapData.dispose();
		}
		this._group = null;
		this._bitmap = null;
		this._textField = null;
		this._textFormat = null;
	}
	
	
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	
	
	
	// 文本标签
	public function set label(value:String):void
	{
		if (this._label == value)
		{
			return;
		}
		this._label = value;
		if (this._label != null && this._label != '')
		{
			_hasLabel = true;
			if (!this._textField)
			{
				this._textField = new TextField();
				this._textField.mouseEnabled = false;
				this._textField.selectable = false;
				this._textField.autoSize = TextFieldAutoSize.LEFT;
				addChild(this._textField);
			}
			this._textField.text = value;
			update();
		}
		else
		{
			_hasLabel = false;
			if (this._textField) 
			{
				removeChild(this._textField);
				this._textField = null;
			}
		}

	}
	
	public function get label():String
	{
		return _label;
	}
	
	// id
	public function set id(value:String):void
	{
		this._id = value;
		if (_hasLabel)
		{
			this.label = id;
		}
	}
	
	public function get id():String
	{
		return this._id;
	}
	
	// 所在group
	public function set group(value:GroupBase):void
	{
		_group = value;
	}
	
	public function get group():GroupBase
	{
		return _group;
	}
	
	// 文本距离组件中心的水平距离
	public function set padding(value:Number):void
	{
		if (this._padding != value) {
			this._padding = value;
			update();
		}
	}
	
	public function get padding():Number
	{
		return this._padding;
	}
	
	// 按钮文本
	public function get textField():TextField
	{
		return this._textField;
	}
	
	
	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
	
	
	
	
	/**
	 * 初始化子元素
	 **/
	override protected function _addChildren():void
	{
		this._bitmap = new Bitmap();
		addChild(this._bitmap);
	}
	
	/**
	 * 绘制
	 **/
	override protected function _draw():void
	{
		if (_textField)
		{
			_rejust();
		}
		if (_skin)
		{
			_bitmap.x = _bitmap.width * -0.5;
			_bitmap.y = _bitmap.height * -0.5;
		}
	}
	
	/**
	 * 调整位置
	 **/
	protected function _rejust():void
	{
		this._textField.x = - this._textField.width * 0.5 + this._padding;
		this._textField.y = - this._textField.height * 0.5;
	}
	
	/**
	 * 更换位图
	 * @param definition		类定义名称
	 **/
	override protected function _skip(definition:String):void
	{
		// 伸缩自在式皮肤由于大小会改变，始终重绘
		if (_skin is RetractileSkin)
		{
			this._bitmap.bitmapData = _getBitmap(definition);
			this._definition = this._skin[definition];
		}
		// 一般皮肤判断使用的是不同位图源才重绘
		else
		{
			if (this._definition != this._skin[definition])
			{
				this._bitmap.bitmapData = _getBitmap(definition);
				this._definition = this._skin[definition];
			}
		}
	}
	
	/**
	 * 绘缺省皮肤
	 * @param fillColor			填充色
	 * @param thickness			边线粗细
	 * @param ellipse			四角内径
	 * @param alpha				透明度
	 * @param lineColor			边线颜色
	 **/
	override protected function _drawNoSkin(fillColor:uint, thickness:Number = 1, ellipse:Number = 2, alpha:Number = 0.9, lineColor:uint = 0 ):void
	{
		this.graphics.clear();
		this.graphics.lineStyle(thickness,lineColor,alpha);
		this.graphics.beginFill(fillColor,alpha);
		this.graphics.drawRoundRect(this._width*-0.5, this._height*-0.5, this._width, this._height, ellipse, ellipse);
		this._isClear = false;
	}
}

}